/*
 * @Description:
 * @Author: yyh
 * @Date: 2022-03-07 00:05:06
 * @LastEditors: yyh
 * @LastEditTime: 2022-04-23 17:35:31
 */
var db = require("../utils/db");
var url = require("url");
var qs = require("querystring");
var fs = require("fs");

//新增使用资产数据
addUserInfo = async (req, res) => {
  var {
    name,
    password,
    role,
    realName,
    gender,
    position,
    telephone,
    email
  } = req.body;
  var sql = "insert into user (name,password,role,realName,gender,position,telephone,email) values(?,?,?,?,?,?,?,?)";
  var sqlArr = [
    name,
    password,
    role,
    realName,
    gender,
    position,
    telephone,
    email
  ];
  var callBack = async (err, data) => {
    if (err) {
      console.log(err);
      res.send({
        code: 400,
        msg: "新增失败"
      })
    } else {
      res.send({
        code: 200,
        msg: "新增成功",
        data: data
      })
    }
  }
  db.sqlConnect(sql, sqlArr, callBack);
}

//删除用户信息数据
deleteUserInfo = async (req, res) => {
  var id = req.query[0];
  var sql = "delete from user where id = ?";
  var sqlArr = [id];
  var result = await db.SySqlConnect(sql, sqlArr);
  if (result.affectedRows == 1) {
    res.send({
      code: 200,
      msg: "删除成功"
    });
  } else {
    res.send({
      code: 400,
      msg: "删除失败"
    });
  }
};

//修改使用资产数据
updateUserInfo = async (req, res) => {
  var {
    id,
    name,
    password,
    role,
    realName,
    gender,
    position,
    telephone,
    email
  } = req.body;
  var sql =
    "update user set name=?,password=?,role=?,realName=?,gender=?,position=?,telephone=?,email=? where id=?";
  var sqlArr = [
    name,
    password,
    role,
    realName,
    gender,
    position,
    telephone,
    email,
    id
  ];
  var result = await db.SySqlConnect(sql, sqlArr);
  if (result.affectedRows == 1) {
    res.send({
      code: 200,
      msg: "修改成功"
    });
  } else {
    res.send({
      code: 400,
      msg: "修改失败"
    });
  }
};

//模糊查询用户信息数据
getUsersInfo = async (req,res) => {
  var params = url.parse(req.url).query;
  params = qs.parse(params);
  var sql = "select * from user";
  var sqlArr = [];
  var isMore = false; //是否有多个查询参数
  if(params.name){
      sql += " where name like ?";
      sqlArr.push("%" + params.name + "%");
      isMore = true;
  }
  if(params.role) {
      if(isMore) {
          sql += " and role like ?";
      } else {
          sql += " where role like ?";
      }
      sqlArr.push("%" + params.role + "%");
  }
  var callBack = async (err, data) => {
      if(err) {
          console.log(err);
          res.send({
              code: 400,
              msg: "模糊查询失败"
          })
      } else {
          res.send({
              code: 200,
              msg: "模糊查询成功",
              data: data
          })
      }
  }
  db.sqlConnect(sql,sqlArr,callBack);
};

//获取用户id和角色方法
var getUserId = (name, password) => {
  var sql =
    "select id,role from user where name=? and password=?";
  var sqlArr = [name, password];
  return db.SySqlConnect(sql, sqlArr);
};

//用户登录
login = (req, res) => {
  var { name, password } = req.body;
  if (name && password) {
    var sql = "select * from user where name=? and password=?";
    var sqlArr = [name, password];
    var callBack = async (err, data) => {
      if (err) {
        console.log(err);
        res.send({
          code: 400,
          msg: "出错了",
        });
      } else {
        var result = await getUserId(name, password);
        if (result[0]) {
          data[0].userinfo = result[0];
          res.send({
            code: 200,
            msg: "登录成功",
            data: data[0],
          });
        } else {
          res.send({
            code: 400,
            msg: "用户名或密码错误,请重新输入"
          })
        }
      }
    };
    db.sqlConnect(sql, sqlArr, callBack);
  }
};

//上传用户头像
uploadUserImg = (req, res) => {
    var file = req.file;
    fs.renameSync(
      "./public/uploads/" + file.filename,
      "./public/uploads/" + file.originalname
    );
    // 设置响应类型及编码
    res.set({
      "content-type": "application/json; charset=utf-8",
    });
    var imgUrl = "http://localhost:3000/uploads/" + file.originalname;
    var { id } = req.body;
    var sql = "update user set img=? where id=?";
    var sqlArr = [imgUrl, id];
    db.sqlConnect(sql, sqlArr, (err, data) => {
      if (err) {
        console.log(err);
        throw "出错了";
      } else {
        if (data.affectedRows == 1) {
          res.send({
            code: 200,
            msg: "操作成功",
            url: imgUrl,
          });
        } else {
          res.send({
            code: 400,
            msg: "操作失败",
            url: imgUrl,
          });
        }
      }
    });
};

//通过id查询用户信息数据
getUserInfoById = async (req, res) => {
  var id = req.query[0];
  var sql = "select * from user where id=?";
  var sqlArr = [id];
  var callBack = async (err, data) => {
    if (err) {
      console.log(err);
      res.send({
        code: 400,
        msg: "查询失败"
      })
    } else {
      res.send({
        code: 200,
        msg: "查询成功",
        data: data
      })
    }
  }
  db.sqlConnect(sql, sqlArr, callBack);
}

module.exports = {
  addUserInfo,
  deleteUserInfo,
  login,
  updateUserInfo,
  getUsersInfo,
  getUserInfoById,
  uploadUserImg,
};
